﻿using lib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XQ.Framework.data
{
    public class SingleDB
    {
        private readonly object DB_LOCK = new object();
        private IDataBase db;
        public Func<IDataBase> DBSource { get; set; }
        public IDataBase GetDataBase()
        {
            if (db == null)
            {
                lock (DB_LOCK)
                {
                    if (db == null)
                    {
                        //默认使用外部数据
                        if (DBSource != null)
                        {
                            db = DBSource.Invoke();
                        }
                        if (db == null)
                        {
                            db = DataBaseFactory.getNewConnection(DataBaseType.mysql);
                        }
                    }
                }
            }
            return db;
        }
        public void ReleaseDataBase()
        {
            if (db != null) { db.Release(); db = null; }
        }
    }
}
